package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Comment;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.CommentMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.CommentQueryObject;
import cn.wolfcode.trip.base.service.ICommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements ICommentService{
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo getPageInfo(CommentQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Comment> list = commentMapper.selectAllByTravelId(qo);
        return new PageInfo(list);
    }

    @Override
    public Comment saveTravelComment(Comment comment) {
        //保存用户对游记的评论
        comment.setDate(new Date());
        comment.setType(1);
        commentMapper.insertTravelComment(comment);
        //保存完返回该游记评论
        User user = userMapper.selectByPrimaryKey(comment.getCommentator().getId());
        comment.setCommentator(user);
        return comment;
    }

    @Override
    public int getCommentLikeNums(long commentId) {
        return commentMapper.getCommentLikeNums(commentId);
    }

    @Override
    public Comment get(Long id) {
        return commentMapper.selectCommentByPrimaryKey(id);
    }

    @Override
    public List<Comment> getUserTravelComment(Long userId) {
        return commentMapper.selectUserTravelComment(userId);
    }

    @Override
    public List<Comment> selectCommentMe(String authorNickName) {
        return commentMapper.selectCommentMe(authorNickName);
    }

    @Override
    public List<Comment> selectNoCommentMe(String authorNickName) {
        return commentMapper.selectNoCommentMe(authorNickName);
    }

    @Override
    public void updateCommentMe(String authorNickName,Long[] ids) {
        for (Long id : ids) {
            commentMapper.updateCommentMe(id);
        }

    }
}
